﻿using HandyControl.Controls;
using HandyControl.Data;
using Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using SanmaoSystem.Views;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Xml.Linq;
using CargoType = Models.CargoType;
using MessageBox = System.Windows.MessageBox;

namespace SanmaoSystem.ViewModels
{
    public class CargoTypeViewModel : BindableBase, IRegionMemberLifetime
     {

     
        private string _tag="";
        public string Tag { get { return _tag; } set { SetProperty(ref _tag, value); } }
        private string _name="";
        public string Name { get { return _name; } set { SetProperty(ref _name, value); } }
        private string _id="";
        public string Id { get { return _id; } set { SetProperty(ref _id, value); } }
        private string _dataListShow = "Visible";
        public   string DataListShow { get { return _dataListShow; } set { SetProperty(ref _dataListShow, value); } }
        private readonly IRegionManager _regionManager;
        //模糊查询关键词
        private string _keyWord;
        public string KeyWord { get { return _keyWord; } set { SetProperty(ref _keyWord,value); } }
        //分页总页数
        private int _totalPage;
        public int TotalPage { get { return _totalPage; } set { SetProperty(ref _totalPage, value); } }
        //当前页码
        private int _pageIndex=1;
        //每页显示数据
        private int PageSize = 5;
        public List<CargoType> list;
        public int PageIndex { get { return _pageIndex; } set { SetProperty(ref _pageIndex, value); } }
        //换页命令
        public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd { get => new DelegateCommand<FunctionEventArgs<int>>(Update); }
        //搜索
        public DelegateCommand<string> Search { get => new DelegateCommand<string>(SearchByKeyWord); }
        public ObservableCollection<CargoType> DataList { set; get; }
        //删除
        private DelegateCommand<int> _delete;
        public DelegateCommand<object> Delete { get => new DelegateCommand<object>(DeleteById);  }
        //编辑
        public DelegateCommand<object> Edit { get => new DelegateCommand<object>(OpenEdit); }
        //添加
        public DelegateCommand Insert { get => new DelegateCommand(OpenInsertCargoType);  }
      
        //编辑/保存
        public DelegateCommand Save1 { get => new DelegateCommand(SaveEdit);  }
        //取消
        public DelegateCommand Cancel { get => new DelegateCommand(Cancel1);  }

        private void Cancel1()
        {

             Name = "";
             Tag = "";
             Id = "";
             DataListShow = "Visible";
          

        }
        
      
           
        private void SaveEdit()
        {   CargoTypeIProvider cargoTypeIProvider = new CargoTypeIProvider();
            CargoType cargoType = new CargoType();
            if (Id == "")
            {
             cargoType.Name = Name;
             cargoType.Tag = Tag;
             cargoType.InsertData = DateTime.Now;
             cargoType.MemberId = 0;
             cargoType.MemberName = (string) Application.Current.Properties["UserName"];
             cargoTypeIProvider.Insert(cargoType);
            Name = "";
            Tag = "";
                MessageBox.Show("添加成功！");
            }
            else
            {
                cargoType.Id = int.Parse(Id);
                cargoType.Name = Name;
                cargoType.Tag = Tag;
                cargoTypeIProvider.Update(cargoType);
                Name = "";
                Tag = "";
                 MessageBox.Show("修改成功！");
            }             
            DataListShow = "Visible";
            Refresh(cargoTypeIProvider);
        }

        public void OpenInsertCargoType()
        {
            Name = "";
            Tag = "";
            DataListShow = "Hidden";
        }
         public void OpenEdit(object obj)
        {
            CargoTypeIProvider cargoTypeIProvider = new CargoTypeIProvider();
            int id = (int)obj;
            CargoType cargoType = cargoTypeIProvider.GetById(id);
            Name = cargoType.Name;
            Tag = cargoType.Tag;
            Id = cargoType.Id.ToString();
            DataListShow = "Hidden";
        }
      
        //刷新数据
        public void Refresh(CargoTypeIProvider cargoTypeIProvider )
        {
            list = cargoTypeIProvider.GetAll();
            PageIndex = 1;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }

   

        private void DeleteById(object obj)
        {   
            int id = (int)obj;
            CargoTypeIProvider cargoTypeIProvider = new CargoTypeIProvider();
            cargoTypeIProvider.Delete(id);
            Refresh(cargoTypeIProvider);
        }
        //每次导航都重新加载
        public bool KeepAlive => false;
        //构造方法
        public CargoTypeViewModel(IRegionManager regionManager) {
            DataList = new ObservableCollection<CargoType>();            
            _regionManager=regionManager;
            CargoType car=new CargoType();
            Reload();
           
        }
        public void Reload()
        {
             CargoTypeIProvider cargoTypeIProvider = new CargoTypeIProvider();
             list = cargoTypeIProvider.GetAll();
            //总页数 = (总数据量 + 每页数据量 - 1) / 每页数据量
            TotalPage = (list.Count+PageSize-1)/ PageSize;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }
        public void Update(FunctionEventArgs<int> pageIndex )
        {  
            int start=(pageIndex.Info-1)*PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }

        public void GetList( int start,int end)
        {

            DataList.Clear();
            var ListNow = list.GetRange(start, end-start);
            foreach(var item in ListNow)
            {
                DataList.Add(item);
            }
        }
        public void SearchByKeyWord(string keyword)
        {    

            CargoTypeIProvider cargoTypeIProvider = new CargoTypeIProvider();
            list= cargoTypeIProvider.GetByKeyWord(keyword);
            TotalPage = (list.Count+PageSize-1)/ PageSize;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
         
        }

   

    }
}
